package cn.yhjz.platform.system.config.security;

import cn.yhjz.common.AjaxResponse;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONUtil;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class ExpiredStrategy implements SessionInformationExpiredStrategy {
    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {
        AjaxResponse res = AjaxResponse.fail(HttpStatus.HTTP_INTERNAL_ERROR, "登录过期！");
        HttpServletResponse response = event.getResponse();
        response.setStatus(HttpStatus.HTTP_INTERNAL_ERROR);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSONUtil.toJsonStr(res));
    }
}
